package desginmode.single

/**
 * 线程安全，懒加载(Synchronized注解单例)
 */
class ThreadSafeSingleSync private constructor() {

    fun getName() = "ThreadSafeSingleSync"

    companion object {
        private var instance: ThreadSafeSingleSync? = null

        @Synchronized
        fun get(): ThreadSafeSingleSync {
            if (instance == null) {
                instance = ThreadSafeSingleSync()
            }
            //!!后面跟两个感叹号表示该对象一定不为空
            return instance!!
        }
    }
}

fun main(args: Array<String>) {
    print(ThreadSafeSingleSync.get().getName())
}